package com.yupi.springbootinit.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yupi.springbootinit.mapper.LoginLogMapper;
import com.yupi.springbootinit.model.entity.LoginLog;
import com.yupi.springbootinit.service.LoginLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import java.util.Date;

@Service
@Slf4j
public class LoginLogServiceImpl extends ServiceImpl<LoginLogMapper, LoginLog> implements LoginLogService {

    @Resource
    private LoginLogMapper loginLogMapper;

    @Override
    public boolean saveLoginLog(String userAccount, String userName, String ipAddress) {
        try {
            LoginLog loginLog = new LoginLog();
            loginLog.setUserAccount(userAccount);
            loginLog.setUserName(userName);
            loginLog.setIpAddress(ipAddress);
            loginLog.setLoginTime(new Date());
            Date now = new Date();
            loginLog.setCreateTime(now);
            loginLog.setUpdateTime(now);
            return loginLogMapper.insert(loginLog) > 0;
        } catch (Exception e) {
            log.error("保存登录日志失败: {}", e.getMessage());
            return false;
        }
    }
}